import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:myapp/components/Logger/loggers.dart';
import 'package:myapp/dio/api/other.dart';
import 'package:myapp/dio/api/site.dart';
import 'package:myapp/dio/model/hitokoto.dart';
import 'package:myapp/dio/model/site_info.dart';
import 'package:myapp/page/components/home/visit_most_posts.dart';
import 'package:myapp/page/components/home/wise_saying.dart';

import 'components/home/show_box.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _HomeState();
  }
}

class _HomeState extends State<HomePage> {
  // Future<SiteInfo> siteInfoData = Site.getInfoApi();

  bool isLoading = true;
  SiteInfo siteInfoData =
      SiteInfo(tagCount: 0, postsCount: 0, categoryCount: 0, commentCount: 0);
  Hitokoto? hitokoto;
  final List<String> imgList = [
    'https://storage.taoqilin.cn/blog/937accf0-ac6f-11ee-a149-bfbe709891e2',
    'https://storage.taoqilin.cn/blog/e72bcd90-a87d-11ee-aa06-19ebee8761ab',
    'https://storage.taoqilin.cn/blog/5983ee80-bb8f-11ee-b9c1-d7d88ff544f6',
    'https://storage.taoqilin.cn/blog/e63447d0-a875-11ee-9153-6348d6900924',
  ];
  Future<void> getAllData() async {
    siteInfoData = await Site.getInfoApi();
    try {
      hitokoto = await Other.getHitokoto();
    } catch (e) {
      logger.e(e);
    }
    setState(() {
      isLoading = false;
    });
  }

  @override
  void initState() {
    super.initState();
    getAllData();
  }

  @override
  Widget build(BuildContext context) {
    return isLoading
        ? const Center(child: CircularProgressIndicator())
        : Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 10),
              child: CarouselSlider(
                options: CarouselOptions(
                  autoPlay: true, // 自动播放
                  aspectRatio: 2.0, // 宽高比
                  enlargeCenterPage: true, // 中间图片放大
                ),
                items: imgList
                    .map((item) => Container(
                          child: Center(
                              child: Image.network(item,
                                  fit: BoxFit.cover, width: 1000)),
                        ))
                    .toList(),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ShowBox(title: '文章数', value: siteInfoData.postsCount),
                ShowBox(title: '标签数', value: siteInfoData.tagCount),
                ShowBox(title: '分类数', value: siteInfoData.categoryCount),
                ShowBox(title: '评论数', value: siteInfoData.commentCount),
              ],
            ),
            if (hitokoto != null) WiseSaying(hitokoto: hitokoto!),
            VisitMostPosts()
          ]);
  }
}
